/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.jaxws;

import java.net.URI;
import java.net.URISyntaxException;

import javax.xml.ws.addressing.AddressingBuilder;
import javax.xml.ws.addressing.AddressingConstants;
import javax.xml.ws.addressing.AddressingProperties;
import javax.xml.ws.addressing.EndpointReference;
import javax.xml.ws.addressing.JAXWSAConstants;
import javax.xml.ws.addressing.Relationship;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;

/**
 * @author boudyacho
 */
@Name("outboundAddressing")
@Scope(ScopeType.EVENT)
@Install(value = false, precedence = Install.FRAMEWORK)
@AutoCreate
@BypassInterceptors
public class OutboundAddressing {

	@Logger
	private Log log;

	private AddressingBuilder builder;

	private AddressingConstants constants;

	private AddressingProperties properties;

	@Create
	public void create() throws Exception {
		builder = AddressingBuilder.getAddressingBuilder();
		constants = builder.newAddressingConstants();
		properties = builder.newAddressingProperties();
		MessageContexts.instance().getMessageContext().put(
				JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES_OUTBOUND,
				properties);
		log.debug("Created: {0}", this);
	}

	// ---------------------------------------------------------------

	public void setAction(String action) throws Exception {
		if (StringUtils.isEmpty(action)) {
			properties.setAction(builder.newURI(constants.getAnonymousURI()));
		} else {
			properties.setAction(builder.newURI(action));
		}
	}

	public void setTo(String toAddress) throws URISyntaxException {
		if (StringUtils.isEmpty(toAddress)) {
			properties.setTo(builder.newURI(toAddress));
		} else {
			throw new RuntimeException("To Address can not be empty.");
		}
	}

	public void setReplyTo(String replyToAddress) throws Exception {
		if (StringUtils.isEmpty(replyToAddress)) {
			EndpointReference replyTo = builder.newEndpointReference(new URI(
					replyToAddress));
			properties.setReplyTo(replyTo);
		}
	}

	public void setMessageID(String id) throws Exception {
		if (id == null || id.length() == 0) {
			throw new RuntimeException("There is no current event context.");
		}
		properties.setMessageID(builder.newURI(id));
	}

	public void setRelatesTo(String correlationId) throws Exception {
		Relationship rel = builder.newRelationship(builder
				.newURI(correlationId).getURI());
		log.debug("Relates To: {0}", rel);
		properties.setRelatesTo(new Relationship[] { rel });
	}

	// ---------------------------------------------------------------

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}

	public static OutboundAddressing instance() {
		return (OutboundAddressing) Component
				.getInstance(OutboundAddressing.class);
	}
}
